package com.ncepu.clubmanagement.mapper;

import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ClubManagementRelationMapper {

    @Select("SELECT EXISTS(SELECT 1 FROM club_management_relationship WHERE person_id = #{personId})")
    public boolean isPersonIdExist(Integer personId);

    @Select("SELECT EXISTS(SELECT 1 FROM club_management_relationship WHERE club_id = #{clubId} and person_id = #{personId})")
    public boolean isPersonIdExistByClubId(Integer clubId, Integer personId);

    @Insert("insert into club_management_relationship(person_id, club_id) values (#{personId}, #{clubId})")
    void addClubManager(Integer personId, Integer clubId);

    @Update("update club_management_relationship set person_id = #{managerId} where club_id = #{clubId}")
    void changeClubManagerIdByClubId(Integer managerId, Integer clubId);

    @Select("select club_id from club_management_relationship where person_id = #{personId}")
    List<Integer> selectClubIdByManagerId(Integer personId);

    @Select("SELECT EXISTS(SELECT 1 FROM club_management_relationship cmr JOIN " +
            "club_selection cs ON cmr.club_id = cs.club_id " +
            "WHERE cmr.person_id = #{sourcePersonId} AND cs.person_id = #{targetPersonId})")
    Boolean isExistBySourcePersonIdAndTargetPersonId(Integer sourcePersonId, Integer targetPersonId);

    @Delete("delete from club_management_relationship where club_id = #{clubId}")
    void deleteClubManagerByClubId(Integer clubId);
}
